﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.WinUI.Hr;
using ESOL.WinUI.Sales;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainProductPlanApplicationItemQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private List<SalesProdPlanApplicationItemEntity> mSelectedApplicationItemList;
        public List<SalesProdPlanApplicationItemEntity> SelectedApplicationItemList
        {
            get { return mSelectedApplicationItemList; }
        }
        private bool mOnlyOneSelected;
        #endregion

        #region Variable
        public frmMaintainProductPlanApplicationItemQuery()
        {
            InitializeComponent();
        }
        public frmMaintainProductPlanApplicationItemQuery(bool onlyOneSelected)
        {
            InitializeComponent();
            mOnlyOneSelected = onlyOneSelected;
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colPACKAGE_TYPE, "PT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCHECKING_TYPE, "CHECK", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "PASTATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByLocation(colLOCATION_CODE, "", true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colPRINT_BRAND, typeof(BoolStatus), typeof(int), true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colIS_FINISHED, typeof(BoolStatus), typeof(int), true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUnit, "UNIT", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND PPA.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (string.IsNullOrEmpty(txtApplicationCode.Text) == false)
            {
                condition += " AND PPA.APPLICATION_CODE LIKE '%" + txtApplicationCode.Text + "%'";
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                condition += " AND PPA.APPLICATION_DATE >= CONVERT(DATETIME,'" + dtpApplicationDate.EditValue != null + "')";
            }
            if (dtpApplicationDate.EditValue != null)
            {
                condition += " AND PPA.APPLICATION_DATE <= CONVERT(DATETIME,'" + dtpApplicationDateTo.EditValue != null + "')";
            }
            if (txtApplicationPerson.Tag != null && txtApplicationPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtApplicationPerson.Tag;
                condition += " AND PPA.APPLICATION_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND PPA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                condition += " AND P.PRODUCT_CODE LIKE '%" + txtProductCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtItem_Out.Text) == false)
            {
                condition += " AND PPAI.PRODUCT_SPEC_OUT LIKE '%" + txtItem_Out.Text + "%'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "APPLICATION_CODE DESC";
            DataTable dt = SalesProdPlanApplicationItemBs.GetByCondition(condition, orderBy);
            dt.Columns.Add("IS_SELECTED",typeof(Boolean));
            foreach (DataRow dr in dt.Rows)
            {
                dr["IS_SELECTED"] = false;
            }
            gvDetail.DataSource = dt;
        }
        #endregion

        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
        }
        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_Confirm()
        {
            mSelectedApplicationItemList = new List<SalesProdPlanApplicationItemEntity>();
            DataTable dt = gvDetail.DataSource as DataTable;
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToBoolean(dr["IS_SELECTED"].ToString()))
                {
                    SalesProdPlanApplicationItemEntity entity = new SalesProdPlanApplicationItemEntity(dr);
                    mSelectedApplicationItemList.Add(entity);
                }
            }
            if (mSelectedApplicationItemList == null || mSelectedApplicationItemList.Count == 0)
            {
                MessageBox.Show("请至少选择一个生产计划申请单明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mOnlyOneSelected && mSelectedApplicationItemList.Count > 1)
            {
                MessageBox.Show("只能选择一个生产计划申请单明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn;
                txtApplicationPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void colIsSelected_Click(object sender, EventArgs e)
        {
            if(mSelectedRow==null)
            {
                return;
            }
            if (Convert.ToBoolean(mSelectedRow["IS_SELECTED"].ToString()) == true)
            {
                mSelectedRow["IS_SELECTED"] = false;
            }
            else 
            {
                mSelectedRow["IS_SELECTED"] = true;
            }
        }
        DataRow mSelectedRow;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            mSelectedRow = gridView1.GetDataRow(e.FocusedRowHandle);
        }


    }
}